package com.tyylab.kestrel.framework.example.web.consumer;

import com.google.common.collect.Lists;
import com.tyylab.kestrel.framework.example.api.IHellowWorldApi;
import com.tyylab.kestrel.framework.spring.rpc.feign.annotation.EnableKestrelFeignClient;
import com.tyylab.kestrel.framework.util.ContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Example application demonstrating Feign fallback functionality
 * @author tyylab
 */
@Slf4j
@SpringBootApplication

@EnableKestrelFeignClient(serviceName = "tyylab-provider",apiBasePackages = {"com.tyylab.kestrel.framework.example.api"})
@EnableKestrelFeignClient(serviceName = "user-service2",apiBasePackages = {"com.tyylab.kestrel.framework.example.web.consumer"})
public class FallbackExampleApp implements CommandLineRunner {
    
    public static void main(String[] args) {
        SpringApplication.run(FallbackExampleApp.class, args);
    }

    @Autowired
    private IHellowWorldApi hellowWorldApi;
    
    @Override
    public void run(String... args) throws Exception {
        log.info("=== Feign Fallback Example ===");
        
        // Test normal call (if service is available)
        try {
            log.info("Testing normal call...");
            ContextUtil.setTenantId("tyylab");
            String result1 = hellowWorldApi.sayHi("tyy", 20);
            log.info("Normal call result: {}", result1);
        } catch (Exception e) {
            log.warn("Normal call failed, fallback should be triggered: {}", e.getMessage());
        }
        
        // Test call that will trigger fallback (service unavailable)
        try {
            log.info("Testing call that will trigger fallback...");
            ContextUtil.setTenantId("tyylab");
            Object result2 = hellowWorldApi.sayHi2("tyy23", 230);
            log.info("Fallback call result: {}", result2);
        } catch (Exception e) {
            log.warn("Call failed with exception: {}", e.getMessage());
        }
        
        log.info("=== Fallback Example Completed ===");

        try {
            log.info("Testing call that will trigger fallback...");
            ContextUtil.setTenantId("tyylab");
            Object result2 = hellowWorldApi.sayHi3(Lists.newArrayList("tyy23"), 230);
            log.info("Fallback call result: {}", result2);
        } catch (Exception e) {
            log.warn("Call failed with exception: {}", e.getMessage());
        }

        log.info("=== Fallback Example Completed ===");
    }
} 